﻿using System;
using System.Reflection;
using System.Windows.Forms;

namespace Beifenqi
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary> 
        static void Main()
        {
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var _form1 = new IntranetBackupDevice();
            Application.Run(_form1);
        }

        //https://www.cnblogs.com/a849788087/p/6552977.html
        //引用的dll修改成->不复制
        //资源的dll修改成->嵌入的资源
        //入口点写 AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

        /// <summary>
        /// 为了exe不带dll,所以反射自身的资源(资源dll不要用文件夹)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs e)
        {
            var ea = Assembly.GetExecutingAssembly();
            foreach (var name in ea.GetManifestResourceNames())
            {
                var stream = ea.GetManifestResourceStream(name);
                if (stream != null)
                {
                    try
                    {
                        byte[] assemblyData = new byte[stream.Length];
                        stream.Read(assemblyData, 0, assemblyData.Length);
                        return Assembly.Load(assemblyData);
                    }
                    catch
                    { }
                }
            }
            return null;
        }
    }
}
